package com.tanhua.management.filter;

import com.tanhua.management.service.UserFreezeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Author: sun
 * DateTime: 2021/4/28
 **/

@WebFilter(value = {"user/loginVerification", "movements"})
public class UserFilter implements Filter {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    //执行过滤的方法;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        //对象转换;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //获取token;
        String token = request.getHeader("Authorization");
        //获取用户id;
        String id = "";
        //如果token不为空;
        if (StringUtils.isNotEmpty(token)) {
            //根据token获取用户id;
            id = "";
        }

        //到redis中查询有无该用户的冻结状态;
        //拼接字符串;
        String rangeKey = UserFreezeService.range + id;
        //到redis中查询冻结范围;
        String range = this.redisTemplate.opsForValue().get(rangeKey);
        //如果查询不到数据，说明用户状态正常;
        if (StringUtils.isEmpty(range)) {
            //直接放行;
            filterChain.doFilter(servletRequest, servletResponse);
        }
        //查询到数据后，根据用户被冻结的范围，拦截对应的功能;
        switch (range) {
            case "1":
                //禁止登陆;

                break;
            case "2":
                //禁止发消息;

                break;
            case "3":
                //禁止发动态;

                break;
        }
    }
}
